package io.plus.user_interface.io.server.command.command_handler;

import drds.common.$;
import io.plus.interactive.AsynchronousSocketChannelContext;
import io.plus.user_interface.io.common.Status;
import io.plus.user_interface.io.common.message.MessageHelper;
import io.plus.user_interface.io.common.packet.Command;
import io.plus.user_interface.io.common.packet.MessageRequestMessage;
import io.plus.user_interface.io.common.packet.ResponseMessage;
import io.plus.user_interface.io.common.packet.UserMessage;
import io.plus.user_interface.io.common.protocol.PacketImpl;
import io.plus.user_interface.io.common.utils.ImKit;
import io.plus.user_interface.io.common.utils.JsonKit;
import io.plus.user_interface.io.server.command.AbstractCommandHandler;

/**
 * 获取聊天消息命令处理器
 */
public class MessageReqHandler extends AbstractCommandHandler {

    @Override
    public Command command() {

        return Command.get_message_request;
    }

    /**
     * 获取用户消息失败响应包;
     *
     * @param asynchronousSocketChannelContext
     * @return
     */
    public PacketImpl getMessageFailedPacket(AsynchronousSocketChannelContext asynchronousSocketChannelContext) {
        ResponseMessage resPacket = new ResponseMessage(Command.get_message_response, Status.C10015);
        return ImKit.convertResponsepPacket(resPacket, asynchronousSocketChannelContext);
    }

    @Override
    public PacketImpl handler(PacketImpl packet, AsynchronousSocketChannelContext asynchronousSocketChannelContext) throws Exception {
        ResponseMessage resPacket = null;
        MessageRequestMessage messageReqBody = null;
        try {
            messageReqBody = JsonKit.toBean(packet.getBody(), MessageRequestMessage.class);
        } catch (Exception e) {
            //用户消息格式不正确
            return getMessageFailedPacket(asynchronousSocketChannelContext);
        }
        UserMessage messageData = null;
        MessageHelper messageHelper = userConfiguration.getMessageHelper();
        //群组ID;
        String groupId = messageReqBody.getGroupId();
        //当前用户ID;
        String userId = messageReqBody.getUserId();
        //消息来源用户ID;
        String fromUserId = messageReqBody.getFromUserId();
        //消息区间开始时间;
        Double beginTime = messageReqBody.getBeginTime();
        //消息区间结束时间;
        Double endTime = messageReqBody.getEndTime();
        //分页偏移量;
        Integer offset = messageReqBody.getOffset();
        //分页数量;
        Integer count = messageReqBody.getCount();
        //消息类型;
        int type = messageReqBody.getType();
        //如果用户ID为空或者type格式不正确，获取消息失败;
        if ($.isNullOrEmpty(userId) || (0 != type && 1 != type)) {
            return getMessageFailedPacket(asynchronousSocketChannelContext);
        }
        if (type == 0) {
            resPacket = new ResponseMessage(Command.get_message_response, Status.C10016);
        } else {
            resPacket = new ResponseMessage(Command.get_message_response, Status.C10018);
        }
        //群组ID不为空获取用户该群组消息;
        if (!$.isNullOrEmpty(groupId)) {
            //离线消息;
            if (0 == type) {
                messageData = messageHelper.getGroupOfflineMessage(userId, groupId);
                //历史消息;
            } else if (1 == type) {
                messageData = messageHelper.getGroupHistoryMessage(userId, groupId, beginTime, endTime, offset, count);
            }
        } else if ($.isNullOrEmpty(fromUserId)) {
            //获取用户所有离线消息(好友+群组);
            if (0 == type) {
                messageData = messageHelper.getFriendsOfflineMessage(userId);
            } else {
                return getMessageFailedPacket(asynchronousSocketChannelContext);
            }
        } else {
            //获取与指定用户离线消息;
            if (0 == type) {
                messageData = messageHelper.getFriendsOfflineMessage(userId, fromUserId);
                //获取与指定用户历史消息;
            } else if (1 == type) {
                messageData = messageHelper.getFriendHistoryMessage(userId, fromUserId, beginTime, endTime, offset, count);
            }
        }
        resPacket.setData(messageData);
        return ImKit.convertResponsepPacket(resPacket, asynchronousSocketChannelContext);
    }
}
